<?php
namespace app\api\controller\system;


use app\api\controller\Api;
use think\db\exception\PDOException;

class Config extends Api
{
    public function read()
    {
        $model = new \app\common\config\model\ConfigSystem();
        $rows = $model->select();

        $result = [];
        foreach ($rows as $row) {
            $result[$row->title] = $row->content;
        }

        return success($result);
    }

    public function save()
    {
        $post = $this->request->post();
        $configSystemLogic = new \app\common\config\logic\ConfigSystem();

        $data = [];
        $data['title'] = $post['type'];
        $data['content'] = $post['data'];
        $model = new \app\common\config\model\ConfigSystem();
        try {
            $model->save($data);
        } catch (PDOException $e) {
            $row = $model->where('title', $post['type'])->find();
            $row->save($data);
            // 更新缓存
            $configSystemLogic->resetConfigItemCache($post['type'], $post['data']);
        }
        return success([], '保存成功');
    }


}